package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.dto.SkuDTO;
import com.leyou.dto.SpuDTO;
import com.leyou.dto.SpuDetailDTO;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuDTO>> querySpuByPage(
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,
            @RequestParam(value = "key",required = false)String key,
            @RequestParam(value = "saleable",required = false)Boolean saleable
    ){
        PageResult<SpuDTO> pageResult = goodsService.querySpuByPage(page, rows, saleable, key);
        return ResponseEntity.ok(pageResult);
    }

    @PostMapping("/goods")
    public ResponseEntity<Void> saveGood(@RequestBody SpuDTO spuDTO){
        goodsService.saveGood(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping("/spu/saleable")
    public ResponseEntity<Void> updateGoodById(@RequestParam(value = "id")Long id,
                                               @RequestParam(value = "saleable")Boolean saleable){
        goodsService.updateGoodById(id,saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> findSpuDetailById(@RequestParam(value = "id")Long id){
        SpuDetailDTO spuDetailDTO = goodsService.findSpuDetailById(id);
        return ResponseEntity.ok(spuDetailDTO);
    }

    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> findSkuById(@RequestParam(value = "id")Long id){
        List<SkuDTO> list =  goodsService.findSkuById(id);
        return ResponseEntity.ok(list);
    }

    @PutMapping("/goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spu){
        goodsService.updateGood(spu);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> findSpuBySpuId(@PathVariable("id") Long id){
        SpuDTO spuDTO = goodsService.findSpuBySpuId(id);
        return  ResponseEntity.ok(spuDTO);
    }

    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> findSkusBySkuIds(@RequestParam(value = "ids") List<Long> ids ){
       List<SkuDTO> list =  goodsService.findSkusBySkuIds(ids);
        return ResponseEntity.ok(list);
    }

    @PutMapping("/stock/minus")
    public ResponseEntity<Void> minusStock(@RequestBody Map<Long,Integer> map){
        goodsService.minusStock(map);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }



 }
